import 'package:flutter/material.dart';
/**
 * 搜索组件，有状态的，有三种表现形式：
 * 1. home 默认状态的样式
 * 2. homeLight 首页发生上滑后使用的样式
 * 3. normal 为默认情况下使用的样式
 * widget 布局如下：
 * 1. 左侧返回按钮 - 可能没有
 * 2. 文本框区域
 *  2.1. 搜索图标
 *  2.2. 文本区域
 *  2.3. 清除按钮
 * 3. 搜索按钮
 */
// 三种状态的枚举值
enum SearchBarType { home, normal, homeLight }

class SearchBarWidget extends StatefulWidget {
  final bool? hideLeft;// 隐藏左侧的返回键
  final SearchBarType searchBarType;// 搜索组件的样式
  final String? hint;// 提示文案
  final String? defaultText;// 默认值
  final void Function()? leftBtnClick;// 左侧按钮的回调函数
  final void Function()? rightBtnClick;// 右侧按钮的回调函数
  final void Function()? inputBoxClick;// 输入框点击回调函数
  final ValueChanged<String>? onChanged;// 输入框内容变化的回调

  const SearchBarWidget({super.key, this.hideLeft, this.searchBarType = SearchBarType.normal, this.hint, this.defaultText, this.leftBtnClick, this.rightBtnClick, this.inputBoxClick, this.onChanged});

  @override
  State<SearchBarWidget> createState() => _SearchBarWidgetState();
}

class _SearchBarWidgetState extends State<SearchBarWidget> {
  //#region 成员变量
  bool showClear = false;// 文本框右边清除按钮
  final TextEditingController _controller = TextEditingController();
  //#endregion
  //#region 初始化
  @override
  void initState() {
    super.initState();
    if(widget.defaultText != null)
      _controller.text = widget.defaultText ?? '';
  }
  //#endregion
  //#region 返回widget
  @override
  Widget build(BuildContext context) {
    Widget ret = widget.searchBarType == SearchBarType.normal ? _normalSearchBar : _homeSearchBar;
    return ret;
  }
  //#endregion

  //#region 常规样式
  Widget get _normalSearchBar => Row(
    children: [
      _leftBackBtn,// 左侧返回按钮
      Expanded(child: _inputbox),// 中间文本框
      _rightSearchBtn,// 右侧搜索按钮
    ],
  );
  //#endregion
  //#region 左侧按钮
  get _backBtn => widget.hideLeft ?? false ? null : Icon(Icons.arrow_back_ios,color: Colors.grey,size:26);
  Widget get _leftBackBtn => _wrapTap(
      Padding(
        padding: const EdgeInsets.fromLTRB(6, 5, 10, 5),
        child: _backBtn,
      ),
      widget.leftBtnClick
  );
  //#endregion
  //#region 文本框
  get _inputbox {
    Color inputColor;
    if(widget.searchBarType == SearchBarType.home) inputColor = Colors.white;
    else inputColor = const Color(0xffededed);

    return Container(
      height: 30,
      padding: const EdgeInsets.fromLTRB(10, 0, 10, 0),
      decoration: BoxDecoration(
        color: inputColor,
        borderRadius: BorderRadius.circular(widget.searchBarType == SearchBarType.normal ? 5 : 15),
      ),
      child: Row(
        children: [
          Icon(Icons.search, size: 20, color: widget.searchBarType == SearchBarType.normal ? const Color(0xffa9a9a9) : Colors.blue,),
          Expanded(child: _textField),
          // 右边的清除按钮
          if(showClear) _wrapTap(const Icon(Icons.clear,size: 22,color: Colors.grey), (){
            setState(() {
              _controller.clear();
            });
            _onChanged('');// 清除文本框内容
          })
        ],
      ),
    );
  }
  //#endregion
  //#region 右侧搜索按钮
  get _rightSearchBtn => _wrapTap(
      const Padding(
        padding: EdgeInsets.fromLTRB(10, 5, 10, 5),
        child: Text('搜索',style: TextStyle(color: Colors.blue, fontSize: 17)),
      ),
      widget.rightBtnClick
  );
  //#endregion
  //#region 主页搜索样式
  get _homeFontColor => widget.searchBarType == SearchBarType.homeLight ? Colors.black54 : Colors.white;
  get _homeSearchBar => Row(
    children: [
      _wrapTap(
        Container(
          padding: const EdgeInsets.fromLTRB(6, 5, 5, 5),
          child: Row(
            children: [
              Text('上海', style: TextStyle(color: _homeFontColor,fontSize: 18)),
              Icon(Icons.expand_more, color: _homeFontColor,size: 22)
            ],
          ),
        ),
        widget.leftBtnClick
      ),
      Expanded(child: _inputbox),// 文本框
      _wrapTap(
        Padding(
          padding: const EdgeInsets.fromLTRB(10, 5, 10, 5),
          child: Text('登出',style: TextStyle(color: _homeFontColor, fontSize: 18)),
        ),
        widget.rightBtnClick
      )
    ],
  );
  //#endregion

  //#region 文本框容器中的文本widget
  get _textField => widget.searchBarType == SearchBarType.normal ? TextField(
    cursorColor: Colors.blue,cursorHeight: 20,
    autofocus: true,style: const TextStyle(fontSize: 18, color: Colors.black, fontWeight: FontWeight.w300),
    controller: _controller, onChanged: _onChanged,
    // 输入框的文本样式
    decoration: InputDecoration(
      contentPadding: const EdgeInsets.only(left: 5, bottom: 15, right: 5),
      border: InputBorder.none,
      hintText: widget.hint,
      hintStyle: const TextStyle(fontSize: 15)
    ),
  ) : _wrapTap(
      Text(widget.defaultText ?? '',style: TextStyle(fontSize: 13, color: Colors.grey)),
      widget.inputBoxClick
  );
  //#endregion

  //#region 为组件包装点击事件
  _wrapTap(Widget? child, void Function()? callback){
    return GestureDetector(onTap: callback, child: child);
  }
  //#endregion

  void _onChanged(String value) {
    bool? show;
    if(value.isNotEmpty) show = true;
    else show = false;
    setState(() {
      showClear = show ?? false;
    });

    if(widget.onChanged != null)
      widget.onChanged!(value);
  }
}
